/**
 * 订单数据模型
 * @type {Order}
 */
/***************/
module.exports = class Order extends require('./model') {

  /**
   * 添加订单
   * @param order
   * @returns {Promise<unknown>}
   */
  static setOrder(order) {
    return new Promise((resolve, reject) => {
      let sql = 'INSERT INTO `order` SET ?';

      console.log('插入订单' + sql);
      console.log('code = ' + order.code + '; user_username = ' + order.user_username +
        '; time = ' + order.time + '; amount = ' + order.amount + ';-------------------');

      this.dbQuery(sql, order)
        .then(result => {
          resolve(result.insertId)
        })
        .catch(error => {
          reject(error)
        })
    })
  }

  /**
   * 获取订单的指定条数
   * @param username 用户名
   * @param code 订单号
   * @param lessAmount 多于的总和
   * @param moreAmount 少于的总和
   * @param lessDate 多余的日期
   * @param moreDate 少于的日期
   * @returns {Promise<unknown>}
   */
  static getOrderCount(username, code, lessAmount, moreAmount, lessDate, moreDate) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT COUNT(1) AS count  FROM \`order\` WHERE 1 = 1 `

      sql += username && username != '' ? ` AND \`order\`.user_username = '${username}' ` : ` `
      sql += code ? ` AND \`order\`.code = '${code}' ` : ` `;
      sql += lessAmount ? ` AND \`order\`.amount >= ${lessAmount} ` : ` `;
      sql += moreAmount && moreAmount != 0 ? ` AND \`order\`.amount <= ${moreAmount} ` : ` `;
      sql += lessDate ? ` AND \`order\`.time >= '${lessDate}' ` : ` `;
      sql += moreDate ? ` AND \`order\`.time <= '${moreDate}' ` : ` `;

      console.log('查询订单数量sql=' + sql);
      console.log('username=' + username + '; code=' + code +
        '; lessAmount=' + lessAmount + '; moreAmount=' + moreAmount + '; lessDate=' + lessDate +
        '; moreDate' + moreDate + ';-----------------------');

      this.dbQuery(sql)
        .then(result => {
          resolve(result[0]);
        })
        .catch(error => {
          reject(error);
        })

    })
  }


  /**
   * 查询订单
   * @param start 开始
   * @param size 条数
   * @param username 用户名
   * @param code 订单号
   * @param lessAmount 多于的总和
   * @param moreAmount 少于的总和
   * @param lessDate 多余的日期
   * @param moreDate 少于的日期
   * @returns {Promise<unknown>}
   */
  static getOrder(start, size, username, code, lessAmount, moreAmount, lessDate, moreDate) {
    return new Promise((resolve, reject) => {
      let sql = `SELECT \`order\`.*  FROM \`order\` WHERE 1 = 1 `

      sql += username && username != '' ? ` AND \`order\`.user_username = '${username}' ` : ` `
      sql += code ? ` AND \`order\`.code = '${code}' ` : ` `;
      sql += lessAmount ? ` AND \`order\`.amount >= ${lessAmount} ` : ` `;
      sql += moreAmount && moreAmount != 0 ? ` AND \`order\`.amount <= ${moreAmount} ` : ` `;
      sql += lessDate ? ` AND \`order\`.time >= '${lessDate}' ` : ` `;
      sql += moreDate ? ` AND \`order\`.time <= '${moreDate}' ` : ` `;

      sql += ` ORDER BY \`order\`.amount DESC LIMIT ?, ? `

      console.log('查询订单sql=' + sql);
      console.log('start=' + start + '; size=' + size + '; username=' + username + '; code=' + code +
        '; lessAmount=' + lessAmount + '; moreAmount=' + moreAmount + '; lessDate=' + lessDate +
        '; moreDate' + moreDate + ';-----------------------');

      this.dbQuery(sql, [start, size])
        .then(result => {
          resolve(result)
        })
        .catch(error => {
          reject(error)
        })
    })
  }
}